/* 
 * This code is part of the smartlink project, see
 * 
 * 		http://code.google.com/p/smartlink/.
 * 
 * Copyright (C) 2008 Oliver Noelle
 * 
 * SmartLink is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SmartLink is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with SmartLink.  If not, see <http://www.gnu.org/licenses/>.
 **/
package org.smartlink;

import java.util.Iterator;
import java.util.LinkedList;

import org.smartlink.search.BestMatchEntry;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

/** Adapts the list of best matches to the ListView. */
public class BestMatchAdapter extends ArrayAdapter<String> {

	private Activity mContext;
	private LinkedList<BestMatchEntry> mBestMatchesList;

	public BestMatchAdapter(Activity activity, int layout, LinkedList<BestMatchEntry> linkedList) {
		super(activity, layout);
		mContext = activity;
		mBestMatchesList = linkedList;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return new BestMatchView(mContext, position, mBestMatchesList.get(position));
	}

	@Override
	public int getCount() {
		return mBestMatchesList.size();
	}

	/**
	 * 
	 * @param linkId
	 *            the database id
	 * @return the position in the list view, -1 if not found
	 */
	public int getPositionOf(String linkId) {
		int pos = 0;
		for (Iterator<BestMatchEntry> iter = mBestMatchesList.iterator(); iter.hasNext();) {
			BestMatchEntry entry = (BestMatchEntry) iter.next();
			Link link = (Link) entry.objPtr;
			if (link.id.equals(linkId)) {
				return pos;
			}
			pos++;
		}
		return -1;
	}

	/**
	 * 
	 * @param linkId
	 *            the database id
	 * @return the position in the list view, -1 if not found
	 */
	public int getCursorPosOf(String linkId) {
		int pos = 0;
		for (Iterator<BestMatchEntry> iter = mBestMatchesList.iterator(); iter.hasNext();) {
			BestMatchEntry entry = (BestMatchEntry) iter.next();
			Link link = (Link) entry.objPtr;
			if (link.id.equals(linkId)) {
				return link.cursorPos;
			}
			pos++;
		}
		return -1;
	}
}
